// pages/good-detail/good-detail.js
var WxParse = require('../../wxParse/wxParse.js');
//获取应用实例
var app = getApp();

Page({
  data:{
    isShow: false,
    num: 1,
    number:1,
    dcolor:'请选择颜色',
    dsize:'请选择尺码'
  },
  /**
   * 跳转页面
   */
  toPage: function(e){
    var targetUrl = e.currentTarget.dataset.url;
    wx.switchTab({
      url: targetUrl,
    })
  },
  /**
   * 是否显示
   */
  hideModal: function(e){
    this.isShow = false;
    this.setData({
      isShow: this.isShow
    });
  },
  showModal: function(e){
    this.isShow = true;
    this.setData({
      isShow: this.isShow
    });
  }, 
  shopadd2:function(){
    this.setData({
      isShow: true
    });
  },
  selectColor:function(e){
    var id = e.currentTarget.id
    var val = e.currentTarget.dataset.value
    this.setData({
      color:id,
      dcolor:val
    })
  },
  selectSize: function (e) {
    var id = e.currentTarget.dataset.index
    var val = e.currentTarget.dataset.value
    this.setData({
      size: id,
      dsize: val
    })
  },
  //加减购物车数量
  dec: function (e) {
    var num = e.target.dataset.num
    if (num > 1) {
      num--;
    }
    this.setData({
      number: num,
    });
  },
  add: function (e) {
    var num = e.target.dataset.num
    num++;
    this.setData({
      number: num,
    });
  },
  //加入购物车
  shopadd:function(e){
    var url = app.data.url
    var formdata = e.detail.value
    wx.request({
      url: '' + url + '/shop/shoppingcart/add',
      data: formdata,
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        if (res.data.status=='ok'){
          wx.switchTab({
            url: '../shopping-cart/shopping-cart',
          })
        }else{
          
        }
      },
      error: function (res) {
        console.log(res)
      }
    })
  },
  onLoad:function(options){
    var id=options.id
    var url=app.data.url
    var that=this
    //商品详情
    wx.request({
      url: '' + url + '/Shop/goods/GetGoodDetail',
      data: { goodsid: id },
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        console.log(res.data)
        var plist = res.data.Coment
        var glist = res.data.Goods
        var slist = res.data.Shop
        that.setData({
          detail:res.data,
          url:url
        })
        // if (res.data.Rows.Content == null) {
        //   var article = '<p style="color:gray;line-height:30px">暂无相关信息</p>'
        // } else {
        //   var article = res.data.Rows.Content
        // }
        /** 
       * WxParse.wxParse(bindName , type, data, target,imagePadding) 
       * 1.bindName绑定的数据名(必填) 
       * 2.type可以为html或者md(必填) 
       * 3.data为传入的具体数据(必填) 
       * 4.target为Page对象,一般为this(必填) 
       * 5.imagePadding为当图片自适应是左右的单一padding(默认为0,可选) 
       */
        //富文本解析
        // WxParse.wxParse('article', 'html', article, that, 0);
        // that.setData({
        //   infodetail: res.data.Rows,
        // })
      },
      error: function (res) {
        console.log(res)
      }
    })
    //规格颜色
    wx.request({
      url: '' + url + '/Shop/Goods/GetGoodsColors',
      data: { id: id },
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        console.log(res.data)
        that.setData({
          style:res.data
        })
      },
      error: function (res) {
        console.log(res)
      }
    })
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  }
})